<?php
    require 'common.php';
    
    $db = new Database();
    $user = getUser($db);
    
    // Error check
    if($user['group'] < 2) {
        redirect('login.php');
        exit();
    }
    if(!isset($_GET['id']))
        throw new Exception('Bad request.');
    
    // Check that the block exists
    $result = $db->query('SELECT id FROM '.
        config('DB_PREFIX').'blocks WHERE id='.intval($_GET['id']));
    if(mysqli_num_rows($result) < 1)
        throw new Exception('No such block.');
    
    $page = new HtmlPage();
    $page->writeHeader(lang('EDITBLOCK_PAGENAME'));
    
    // Java script
    $page->write('<script>', PUSH_TAB);
    $page->write('var next_qid=1;');
    
    // checkempty()
    $page->write('function checkEmpty(fs) {', PUSH_TAB);
    $page->write('for(var i=0; i<fs.childNodes.length; i++) {', PUSH_TAB);
    $page->write('if(!fs.childNodes[i].tagName || fs.childNodes[i].tagName.toLowerCase() != \'textarea\') continue;');
    $page->write('if(fs.childNodes[i].value != \'\') return false;');
    $page->write('}', POP_TAB);
    $page->write('return true;');
    $page->write('}', POP_TAB);
    
    // addNewFs(ebform)
    $page->write('function addNewFs(ebform) {', PUSH_TAB);
    $page->write('var newfs = document.createElement(\'fieldset\');');
    $page->write('newfs.innerHTML = \''.
        '<legend>'.lang('EDIT_QUESTION').'</legend>'.
        '<p>'.lang('QUESTION').'</p>'.
        '<textarea name="q_new_\'+next_qid+\'"></textarea>'.
        '<p>'.lang('ANSWER').'</p>'.
        '<textarea name="a_new_\'+next_qid+\'"></textarea>'.
        '\';');
    $page->write('ebform.appendChild(newfs);');
    $page->write('next_qid++;');
    $page->write('}', POP_TAB);
    
    // numFieldset
    $page->write('function numFieldset(ebform) {', PUSH_TAB);
    $page->write('var kk=0;');
    $page->write('for(var i=0; i<ebform.childNodes.length; i++) {', PUSH_TAB);
    $page->write('if(!ebform.childNodes[i].tagName || ebform.childNodes[i].tagName.toLowerCase() != \'fieldset\') continue;');
    $page->write('kk++;');
    $page->write('}', POP_TAB);
    $page->write('return kk;');
    $page->write('}', POP_TAB);

    // formChange()
    $page->write('function formChange() {', PUSH_TAB);
    $page->write('var ebform = document.getElementById(\'editblockform\');');
    $page->write('var nfs = numFieldset(ebform);');
    $page->write('var kk = -1;');
    $page->write('for(var i=0; i<ebform.childNodes.length; i++) {', PUSH_TAB);
    $page->write('if(!ebform.childNodes[i].tagName || ebform.childNodes[i].tagName.toLowerCase() != \'fieldset\') continue;');
    $page->write('kk++;');
    // Remove if empty and not last
    $page->write('if( checkEmpty( ebform.childNodes[i]) && kk != (nfs-1)) {', PUSH_TAB);
    $page->write('ebform.removeChild( ebform.childNodes[i] );');
    $page->write('return;');
    $page->write('}', POP_TAB);
    // Add if last and not empty
    $page->write('if( !checkEmpty( ebform.childNodes[i]) && kk == (nfs-1)) {', PUSH_TAB);
    $page->write('addNewFs(ebform);');
    $page->write('return;');
    $page->write('}', POP_TAB);
    // formchange end
    $page->write('}', POP_TAB);
    $page->write('}', POP_TAB);
    $page->write('</script>', POP_TAB);
    
    $page->writeTitle();
    $page->writeMenu('EDITBLOCK', $user);
    $page->write('<div class="content">', PUSH_TAB);
    $page->write('<h2>'.lang('EDITBLOCK_PAGENAME').'</h2>');
    $page->write('<form id="editblockform" onkeyup="formChange()" action="doeditblock.php" method="post">', PUSH_TAB);
    $page->write(getCsrfGuard());
    $page->write('<input type="hidden" name="id" value="'.intval($_GET['id']).'" />');
    $page->write('<input type="submit" value="'.lang('SAVE').'" />');
    
    // Get the block
    $result = $db->query('SELECT id, question, answer FROM '.
        config('DB_PREFIX').'questions WHERE block='.intval($_GET['id']));
    while($row = mysqli_fetch_assoc($result)) {
        $page->write('<fieldset>', PUSH_TAB);
        $page->write('<legend>'.lang('EDIT_QUESTION').'</legend>');
        $page->write('<p>'.lang('QUESTION').'</p>');
        $page->write(
            '<textarea name="q_old_'.$row['id'].'">'.
            htmlEscape( $row['question'] ).
            '</textarea>');
        $page->write('<p>'.lang('ANSWER').'</p>');
        $page->write(
            '<textarea name="a_old_'.$row['id'].'">'.
            htmlEscape( $row['answer'] ).
            '</textarea>');
        $page->write('</fieldset>', POP_TAB);
    }
    
    // New question
    $page->write('<fieldset>', PUSH_TAB);
    $page->write('<legend>'.lang('EDIT_QUESTION').'</legend>');
    $page->write('<p>'.lang('QUESTION').'</p>');
    $page->write(
        '<textarea name="q_new_0">'.
        '</textarea>');
    $page->write('<p>'.lang('ANSWER').'</p>');
    $page->write(
        '<textarea name="a_new_0">'.
        '</textarea>');
    $page->write('</fieldset>', POP_TAB);
    
    $page->write('</form>', POP_TAB);
    $page->write('</div>', POP_TAB);
    $page->writeFooter();
?>